package com.yj.cloudnote.service.Impl;

import com.mongodb.DBObject;
import com.yj.cloudnote.mongo.dao.FilesDao;
import com.yj.cloudnote.service.FilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class FilesServiceImpl implements FilesService {
    @Autowired
    private FilesDao filesDao;

    @Override
    public Boolean initFiles(DBObject dbObject) {
        filesDao.initFiles(dbObject);
        return true;
    }

    @Override
    public Boolean addNewFile(String username, int fileId) {
        Map<String,Object> params = new HashMap<>();
        params.put("username",username);
        params.put("file_id",fileId);
        params.put("content","");
        filesDao.insertOne(params);
        return true;
    }

    @Override
    public Boolean updataFile(String username, int fileId, String content) {
        Map<String,Object> where = new HashMap<>();
        where.put("username",username);
        where.put("file_id",fileId);
        Map<String,Object> set = new HashMap<>();
        set.put("content",content);
        filesDao.updataOne(where,set);
        return true;
    }

    @Override
    public String getFileContent(String username, int fileId) {
        Map<String,Object> query = new HashMap<>();
        query.put("username",username);
        query.put("file_id",fileId);
        Map<String,Object> key = new HashMap<>();
        key.put("content",1);
        DBObject dbObject = filesDao.getOne(query,key);
        String content = (String) dbObject.get("content");
        return content;
    }
}
